package models;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class DBConnect {

    public static Connection con = null;
    public static Statement stm;
    private static String username;
    private static String password;
    private static String hostname;
    private static String port;
    private static String database;

    public static Connection getConn() {
        return con;
    }

    public static String getDatabase() {
        return database;
    }

    public static String getHostname() {
        return hostname;
    }

    public static String getPassword() {
        return password;
    }

    public static String getPort() {
        return port;
    }

    public static String getUsername() {
        return username;
    }

    // Doc du lieu ket noi toi co so du lieu
    public static void readConfig() {
        try {
            File fXmlFile = new File("config.xml");
            if (fXmlFile.exists()) {
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(fXmlFile);
                doc.getDocumentElement().normalize();

                NodeList nList = doc.getElementsByTagName("connection");

                for (int temp = 0; temp < nList.getLength(); temp++) {

                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                        Element eElement = (Element) nNode;

                        hostname = getTagValue("hostname", eElement);
                        port     = getTagValue("port", eElement);
                        username = getTagValue("username", eElement);
                        password = getTagValue("password", eElement);
                        database = getTagValue("database", eElement);

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Lay gia tri theo tag name
    private static String getTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

        Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();
    }
    
    public static boolean Connect(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            System.out.println("Thieu driver");
            return false;
        }
        readConfig();
          try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port
                    + "/" + database, username, password);
            return true;
        } catch (SQLException ex) {
            return false;
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        return false;
   }
    public static void Close(){
        if (con != null) {
            try {
                con.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
}
